//
//  jxsEditTaskDetailViewController.m
//  ToDoList
//
// This code file is apart of project "iPhone-ToDo-List" 
// (http://code.google.com/p/iphone-todo-list/)
//
// Copyright (c) 2009 Frank Villasenor
//
// Distributed under the terms of the Lesser GNU General Public License.
// A copy of the license should be with these files. If not, you 
// can retrieve a copy from http://www.gnu.org/licenses/lgpl-3.0.txt
// 

#import "jxsEditTaskDetailViewController.h"

#import "tosuTask.h"
#import "Constants.h"
#import "jxsSelectDueDateViewController.h"
#import "ToDoListAppDelegate.h"

#import "jxsTaskReceiver.h"

#define kIsCompleteSegmentControlIncompleteSelected		0

@implementation jxsEditTaskDetailViewController

@synthesize titleTextField, dueDateButton, isCompletedSegControl, notesTextView, updateButton;
@synthesize taskToEdit;
@synthesize selectDueDateViewController;


- (void)dealloc 
{
	[titleTextField release];
	[dueDateButton release];
	[isCompletedSegControl release];
	[notesTextView release];
	[taskToEdit release];
	[updateButton release];
	
	[selectDueDateViewController release];
	
    [super dealloc];
}



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
	changingDueDate = NO;
	
	[dueDateButton stylizeButtonWithStockCustomImages];
	[updateButton stylizeButtonWithStockCustomImages];
	
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
	if( self.taskToEdit == nil )
	{
		[NSException raise:@"taskToEdit is nil" format:@"taskToEdit is nil; can not proceed."];
	}
	else
	{
		if( changingDueDate == NO )
		{
			self.titleTextField.text = taskToEdit.title;
			[self setDueDateButtonTitle]; //couple of things to do on the button...
			self.isCompletedSegControl.selectedSegmentIndex = 
				(taskToEdit.isComplete == kIsCompleteSegmentControlIncompleteSelected) ? 0 : 1;
			self.notesTextView.text = taskToEdit.notes;
			
			//
			// Set the title to "Edit <task name>"
			NSString *localTitle = [[NSString alloc] initWithFormat:@"Edit %@",taskToEdit.title];
			self.title = localTitle;
			[localTitle release];
		}
	}
	
	
	[super viewWillAppear:animated];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark -
#pragma mark Custom Methods
- (void)setDueDateButtonTitle
{
	[self.dueDateButton setTitle:[self.taskToEdit.dueDate getShortDateWithoutTime] forState:UIControlStateNormal];
	[self.dueDateButton setTitle:[self.taskToEdit.dueDate getShortDateWithoutTime] forState:UIControlStateHighlighted];
}

- (IBAction)showSetDueDateView:(id)sender
{
	if( self.selectDueDateViewController == nil )
	{
		jxsSelectDueDateViewController *selectDueDateVC = [[jxsSelectDueDateViewController alloc] initWithNibName:@"jxsSelectDueDateView" bundle:nil];
		self.selectDueDateViewController = selectDueDateVC;
		[selectDueDateVC release];
		
		self.selectDueDateViewController.title = @"Set Due Date";
	}
	
	ToDoListAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	
	//
	// Set the reference to the view controller where about to add this item too...
	self.selectDueDateViewController.containingNavController = delegate.mainNavController;
	self.selectDueDateViewController.selectedDate = taskToEdit.dueDate; //Set the due date.
	
	[delegate.mainNavController pushViewController:self.selectDueDateViewController animated:YES]; 
	changingDueDate = YES;
}

- (IBAction)updateTask:(id)sender
{
	taskToEdit.title = self.titleTextField.text;
	//Due Date has already been set.
	taskToEdit.isComplete = (self.isCompletedSegControl.selectedSegmentIndex != kIsCompleteSegmentControlIncompleteSelected);
	taskToEdit.notes = self.notesTextView.text;
	
	
	ToDoListAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.mainNavController popViewControllerAnimated:YES];
	changingDueDate = NO;
	
	id topViewController = [delegate.mainNavController topViewController];
	if( [topViewController conformsToProtocol:@protocol(jxsTaskReceiver)] )
	{
		[topViewController UpdateLocalTaskWith:self.taskToEdit];
	}
	else
	{
		[NSException raise:@"updateTask: failed" 
			format:@"New topViewController doesn't conform to jxsTaskReceiver!"];
	}
}

- (IBAction)sendKeyboardAway:(id)sender
{
	[titleTextField resignFirstResponder];
	[notesTextView resignFirstResponder];
}

#pragma mark -
#pragma mark jxsDueDateReceiver Protocal Methods
- (void)setDueDateToDate:(NSDate *)newDueDate
{
	self.taskToEdit.dueDate = newDueDate; //Set the due date
	[self setDueDateButtonTitle]; //Update the button text.
	changingDueDate = NO; //<-- because of this the caller must be popped before this method is invoked..
}

@end
